﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using XLua;

public class LineRendererGroup : MonoBehaviour
{
    public int lineNum;// 路线数量
    public float lineWidth;// 路线宽度 因为lineRander宽度超过1 就不好观测了
    public int pointNum;// 路点数量
    public int crossNum;// 交接点数量
    private List<LineRenderer> lines;
    private List<SpriteRenderer> points;
    private List<SpriteRenderer> crosses;

    private Material lineShareMat;
    private Material pointShareMat;
    private Material crossShareMat;
    private LuaFunction luaCallBack;

    public void InitGroup()
    {
        SetAllLines();
        SetAllPoints();
        SetAllCross();
    }

    public void SetLineGroupWidth(float width)
    {
        if (lines != null)
        {
            for (int i = 0; i < lines.Count; i++)
            {
                lines[i].startWidth = width;
                lines[i].endWidth = width;
            }
            lineWidth = width;
        }
    }

    public void SetLineGroupMatTiling_x(float value,string name)
    {
        if (lineShareMat)
        {
            Vector2 scale = lineShareMat.GetTextureScale(name);
            lineShareMat.SetTextureScale(name, new Vector2(value, scale.y));
        }
    }

    public void SetPointGroupScale(float scale)
    {
        if (points != null)
        {
            for (int i = 0; i < points.Count; i++)
            {
                points[i].transform.localScale = Vector3.one * scale;
            }
        }
    }

    public void SetCrossGroupScale(float scale)
    {
        if (crosses != null)
        {
            for (int i = 0; i < crosses.Count; i++)
            {
                crosses[i].transform.localScale = Vector3.one * scale;
            }
        }
    }

    public void LineFade(bool bShow,float time,LuaFunction callBack = null)
    {
        luaCallBack = callBack;
        if (lines != null)
        {
            if (bShow)
            {
                if (lineShareMat)
                {
                    lineShareMat.DOFade(1, time);
                }
                if (pointShareMat)
                {
                    pointShareMat.DOFade(1,time);
                }
                if (crossShareMat)
                {
                    crossShareMat.DOFade(1, time);
                }
            }
            else
            {
                if (lineShareMat)
                {
                    lineShareMat.DOFade(0, time).OnComplete(DoCompleteBack);
                }
                if (pointShareMat)
                {
                    pointShareMat.DOFade(0, time);
                }
                if (crossShareMat)
                {
                    crossShareMat.DOFade(0, time);
                }
            }
            //for (int i = 0; i < lines.Count; i++)
            //{
            //    if (bShow)
            //    {
            //        lines[i].DOColor(hide,show, time);
            //    }
            //    else
            //    {
            //        lines[i].DOColor(show, hide, time).OnComplete(DoCompleteBack);
            //    }

                //}
        }
    }

    private void DoCompleteBack()
    {
        if (luaCallBack != null) luaCallBack.Action(true);
        luaCallBack = null;
    }

    // 编辑用
    public void SetAllLines()
    {
        Transform lineRoot = transform.Find("lineRoot");
        LineRenderer[] lines0 = null;
        if (lineRoot)
        {
            lines0 = lineRoot.GetComponentsInChildren<LineRenderer>();
        }
        else
        {
            lines0 = transform.GetComponentsInChildren<LineRenderer>();
        }
        if (lines0 == null || lines0.Length == 0)
        {
            //Debug.LogError("该路线数量为0，请检查");
        }
        else
        {
            lines = new List<LineRenderer>(lines0);
            lineNum = lines0.Length;
            lineShareMat = lines0[0].sharedMaterial;
        }
    }

    public void SetAllPoints()
    {
        Transform pointRoot = transform.Find("pointRoot");
        if (pointRoot)
        {
            SpriteRenderer[] points0 = pointRoot.GetComponentsInChildren<SpriteRenderer>();
            if (points0 == null || points0.Length == 0)
            {
                //Debug.LogError("该路线路点数量为0，请检查");
            }
            else
            {
                points = new List<SpriteRenderer>(points0);
                pointNum = points0.Length;
                pointShareMat = points0[0].sharedMaterial;
            }
        }
        else
        {
            //Debug.LogError("该路线不存在路点根节点，请检查");
        }
    }

    public void SetAllCross()
    {
        Transform crossRoot = transform.Find("crossRoot");
        if (crossRoot)
        {
            SpriteRenderer[] cross0 = crossRoot.GetComponentsInChildren<SpriteRenderer>();
            if (cross0 == null || cross0.Length == 0)
            {
                //Debug.LogError("该路线路点数量为0，请检查");
            }
            else
            {
                crosses = new List<SpriteRenderer>(cross0);
                crossNum = cross0.Length;
                crossShareMat = cross0[0].sharedMaterial;
            }
        }
        else
        {
            //Debug.LogError("该路线不存在路点根节点，请检查");
        }
    }
}
